﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Bioinformatics.Utilities;

namespace Bioinformatics.Algorithms
{
    public class PsomlCuda : Psoml
    {
        public PsomlCuda()
        {
            AlgorithmName = "PSOML NVIDIA CUDA";
        }

        public override void EvaluateParticles()
        {
            throw new NotImplementedException();
        }

        public override void UpdateParticlePostions()
        {
            throw new NotImplementedException();
        }

        public override void UpdateParticleVelocities()
        {
            throw new NotImplementedException();
        }

        public override Benchmarker GetBenchmarker()
        {
            return new PsomlCudaBenchmarker(this);
        }

        public void LaunchKernel()
        {
            //write the configuration to text file
            //LaunchConfiguration.ToTextFile(Path.Combine(KernelDirectory, "arg.txt"));
            //create new process
            var kernelProcess = new Process();
            //set the process file path
            //kernelProcess.StartInfo.FileName = Path.Combine(KernelDirectory, KernalExecutableName);
            //start the CUDA compiled executable as a separate process
            kernelProcess.Start();
            //wait for the process to finish processing
            kernelProcess.WaitForExit();
            
            //check that program exited ok
            //kernelProcess.ExitCode;
        }

        public override void AutoRun()
        {
            throw new NotImplementedException();
        }
    }

    public class PsomlCudaBenchmarker : Benchmarker
    {
        public PsomlCudaBenchmarker(PsomlCuda psomlCuda)
        {
            Algorithm = psomlCuda;
        }

        public override string ToString()
        {
            return Algorithm.ToString();
        }

        public override void Start()
        {
            throw new NotImplementedException();
        }

        public override void Stop()
        {
            throw new NotImplementedException();
        }

        public override void Pause()
        {
            throw new NotImplementedException();
        }

        public override void Resume()
        {
            throw new NotImplementedException();
        }

        public override void Abort()
        {
            throw new NotImplementedException();
        }
    }

    public class ResultsReader
    {
        public float[] DataPoints { get; set; }

        public static ResultsReader FromCsvFile(string path)
        {
            //create new instance of class
            var resultsReader = new ResultsReader();
            //open the result file into a stream reader
            var streamReader = new StreamReader(path);
            //split each line into an array
            var stringEntries = streamReader.ReadToEnd().Split('\n');
            //allocate memory for datapoints in instance
            resultsReader.DataPoints = new float[stringEntries.Length];
            //read each entry into object
            for (int i = 0; i < stringEntries.Length; i++)
                resultsReader.DataPoints[i] = Convert.ToSingle(stringEntries[i].Split(',')[1]);
            //return the object
            return resultsReader;
        }
    }
}